package Menu;

import Model.Buttons;
import Dialog.ExitDialog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//初始菜单
public class Menu extends JFrame implements ActionListener, MouseListener, MouseMotionListener {
    //    关闭按钮和缩小化按钮
    private Buttons closeButton;
    private Buttons smallButton;
    //    窗口位置
    private Point position;
    //    退出游戏对话框
    protected ExitDialog exitDialog;

    //    关闭图标
    private static final String[] closeIcons = {
            "images/close_icon.png", "images/close_pressed.png", "images/close_pressed.png"
    };
    //    缩小图标
    private static final String[] smallIcons = {
            "images/small_icon.png", "images/small_pressed.png", "images/small_pressed.png"
    };

    /**
     * Menu类的构造函数
     *
     * <p>在Menu类的的构造函数中会对菜单的窗口样式进行初始化
     * 包括窗口的大小和显示位置，最小化和关闭按钮。
     * 生成的Menu实例大小不可以被更改</p>
     */
    public Menu() {
        this.init();
        //    窗口样式设置
        setUndecorated(true);
        setLayout(null);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setBounds(800, 100, 400, 711);
        setVisible(true);
        paintBackground();
        addMouseListener(this);
    }

    /**
     * 菜单头部的初始化函数
     *
     * <p>包括菜单栏的最小化按钮和关闭按钮
     * 用headLabel填充其他空白区域</p>
     */
    private void init() {
        closeButton = new Buttons(closeIcons, null);
        smallButton = new Buttons(smallIcons, null);
        //    导航栏
        JLabel headLabel = new JLabel();
        exitDialog = new ExitDialog(this, "退出游戏", true);

        closeButton.setBounds(370, 0, 30, 30);
        closeButton.addActionListener(this);
        closeButton.setBorderPainted(false);
        smallButton.setBounds(340, 0, 30, 30);
        smallButton.addActionListener(this);
        smallButton.setBorderPainted(false);
        headLabel.setBounds(0, 0, 340, 30);
        headLabel.setBackground(new Color(72, 72, 72));
        headLabel.setOpaque(true);

        headLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
                position = new Point(e.getX(), e.getY());
                setCursor(new Cursor(Cursor.MOVE_CURSOR));
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }
        });
        headLabel.addMouseMotionListener(this);

        this.add(closeButton);
        this.add(smallButton);
        this.add(headLabel);
        this.repaint();
    }

    /**
     * 绘制菜单的背景图片
     */
    public void paintBackground() {
    }

    /**
     * 设置窗口的点击事件
     *
     * <p>当用户点击最小化按钮的时候会将窗口最小化
     * 当用户点击关闭按钮的时候会将窗口关闭</p>
     *
     * @param e 用户点击的按键
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == closeButton) {
            exitDialog.setVisible(true);
            if (exitDialog.getMessage() == ExitDialog.exit) {
                System.exit(0);
            }
        } else if (e.getSource() == smallButton) {
            setExtendedState(ICONIFIED);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    /**
     * 设置窗口拖动事件
     *
     * <p>当用户点击菜单头部栏并拖动的时候会触发事件
     * 窗口会跟随用户的鼠标进行移动</p>
     *
     * @param e 用户鼠标拖动事件
     */
    @Override
    public void mouseDragged(MouseEvent e) {
        Point newPosition = new Point(getLocation().x + e.getX() - position.x,
                getLocation().y + e.getY() - position.y);
        setLocation(newPosition.x, newPosition.y);
    }

    @Override
    public void mouseMoved(MouseEvent e) {

    }
}